import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '@/components/Login'


import Home from '@/components/Home'
//  权限
import Rights from '@/views/rights/rights'


import Roles from '@/views/rights/roles'

//  用户
import Users from '@/views/users/users'

//  商品
import Goods from '@/views/goods/goods'
import GoodsAdd from '@/views/goods/goodsAdd'
import Params from '@/views/goods/params'
import Categories from '@/views/goods/categories'

//   订单
import Order from '@/views/order/order'
import Reports from '@/views/reports/reports'

import headers from '@/comPonent/header'

Vue.use(VueRouter)

const routes = [
  {
    path: '/headers',
    component: headers
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    children: [
      { path: "/rights", component: Rights },

      // { path: "/rights", component:() => import(/* webpackChunkName:"power" */ '@/views/rights/rights')  },


      { path: "/roles", component: Roles },
      { path: "/users", component: Users },
      { path: "/goods", component: Goods },
      { path: "/goods/add", component: GoodsAdd },
      { path: "/params", component: Params },
      { path: "/categories", component: Categories },
      { path: "/orders", component: Order },
      { path: "/reports", component: Reports },
    ]
  }
]

const router = new VueRouter({
  routes,
})


/* 
路由拦截  ====钩子函数
beforeEach   =====前置守卫 【路由值发生改变之前就拦截】
afterEach   =====前置守卫 【路由值发生改变之前就拦截】


to====要去的路径
from==当前的路径
next====函数  放行


*/

router.beforeEach(function (to, from, next) {
  console.log(to, 66666);
  console.log(from, 77777);

  /* 
  1-判断 token 
   如果有token  你已经登陆过   那就放行
   如果没有token  说明没有登录====去登录页面
  
   2-login  如果输入login   就直接放行
  
  */
  if (to.path == '/login') {
    return next()
  }

  var token = window.sessionStorage.getItem('token')
  //  输入的是/home  没有登录
  if (!token) {
    return next("/login")
    // return next({path:'/login'})
  }

  next()
})



//  解决路由版本的兼容性的
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

export default router
